package model;

import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

public class MockUserRepo implements UserRepo {
  private SortedSet<User> users;
  private static MockUserRepo repo = new MockUserRepo();

  private MockUserRepo() {
    this.users = new TreeSet<User>();
    this.addUser("Conrado", "123", "cachi77@gmail.com");
    this.addUser("Lucila", "456", "lucilastancato@gmail.com");
    this.addUser("Andres", "321", "andres@gmail.com");
  }

  public static MockUserRepo getInstance() {    
    return repo;
  }

  @Override
  public SortedSet<User> getUsers() {
    return users;
  }

  @Override
  public void addUser(String name, String pass, String mail) {
    User newuser = new User(name, pass, mail);

    if(!users.contains(newuser)){
      users.add(newuser);
    } else {
      throw new RuntimeException("The user already exists");
    }
  }

  @Override
  public void delUser(int id) {
    // TODO Auto-generated method stub

  }

  @Override
  public User getUserByMail(String mail) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public User getUserByName(String name) {
    Iterator<User> i = this.users.iterator();
    while(i.hasNext()){
      User u = i.next();
      if(u.getName().equals(name)){
        return u; 
      }
    }
    throw new RuntimeException("User does not exist");
  }

  @Override
  public void updateUser(String name, String password, String mail) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public User getUser(String name, String password) {
    User aux = this.getUserByName(name);

    if(!aux.validate(password)){
      throw new RuntimeException("Username/Password incorrect");
    }
    return aux;
  }
}
